/**
 * <p>Title: Snake</p>
 * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p>
 * @author Gavin
 */

package com.deaboway.snake.util;

import javax.microedition.rms.*;

abstract public class BaseRMS {
	private String rmsName;
	private RecordStore recordStore;
	public static boolean FIRST;

	protected BaseRMS(String rmsName) {
		this.rmsName = rmsName;
	}

	public void open() throws Exception {
		try {
			recordStore = RecordStore.openRecordStore(this.rmsName, true);
			if (recordStore.getNumRecords() > 0) {
				FIRST = false;
				loadData();
			} else {
				FIRST = true;
				createDefaultData();
			}
		} catch (Exception e) {
			throw new Exception(this.rmsName + "::open::" + e);
		}
	}

	public void openonly() throws Exception {
		try {
			recordStore = RecordStore.openRecordStore(this.rmsName, true);
		} catch (Exception e) {
			throw new Exception(this.rmsName + "::openonly::" + e);
		}
	}

	public void close() throws Exception {
		if (recordStore != null) {
			try {
				recordStore.closeRecordStore();
			} catch (Exception e) {
				throw new Exception(this.rmsName + "::close::" + e);
			}
		}
	}

	public RecordStore getRecordStore() {
		return this.recordStore;
	}

	public String getRMSName() {
		return this.rmsName;
	}

	abstract void loadData() throws Exception;

	abstract void createDefaultData() throws Exception;

	abstract void updateData() throws Exception;
}
